package cn.tannn.cat.file.api;

import cn.tannn.cat.file.api.constants.FileConstants;
import cn.tannn.cat.file.api.entity.FileIndexVO;
import cn.tannn.cat.file.sdk.api.UploadFile;
import cn.tannn.cat.file.sdk.api.UploadFiles;
import cn.tannn.cat.file.sdk.constants.OSSConstants;
import cn.tannn.cat.file.sdk.enums.StorageDict;
import cn.tannn.jdevelops.result.response.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * rpc 接口
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2024/3/15 14:03
 */
@FeignClient(name = FileConstants.FILE_SERVICES)
public interface FileOperateApi {

    /**
     * master上传
     *
     * @param upload UploadFile
     * @return FileStorageVO
     * @throws IOException Exception
     */
    @PostMapping(value = "upload")
    @Operation(summary = "上传文件", description = "master上传")
    ResultVO<FileIndexVO> upload(@Valid UploadFile upload) throws IOException;


    /**
     * 选择存储器上传
     *
     * @param upload    UploadFile
     * @param storageId 存储器ID
     * @return FileStorageVO
     * @throws IOException Exception
     */
    @PostMapping(value = "upload/{storageId}")
    @Operation(summary = "上传文件", description = "选择存储器上传")
    ResultVO<FileIndexVO> upload(@PathVariable("storageId") Long storageId, @Valid UploadFile upload) throws IOException;


    /**
     * master 批量上传文件
     *
     * @param uploads UploadFiles
     * @return String
     * @throws IOException Exception
     */

    @PostMapping(value = "uploads")
    @Operation(summary = "批量上传文件", description = "master上传")
    ResultVO<String> uploads(@Valid UploadFiles uploads) throws IOException;


    /**
     * master 批量上传文件
     *
     * @param uploads   UploadFiles
     * @param storageId 存储器ID
     * @return String
     * @throws IOException Exception
     */

    @PostMapping(value = "uploads/{storageId}")
    @Operation(summary = "批量上传文件", description = "选择存储器上传")
    ResultVO<String> uploads(@PathVariable("storageId") Long storageId, @Valid UploadFiles uploads) throws IOException;


    /**
     * 下载文件
     *
     * @param fileIndexId 文件索引的ID
     * @param response    HttpServletResponse
     */
    @GetMapping(value = "download")
    @Operation(summary = "下载文件")
    @Parameter(name = "fileStorageId", description = "文件索引的ID", required = true)
    void download(@RequestParam("fileIndexId") Long fileIndexId, HttpServletResponse response);


    /**
     * 删除文件
     *
     * @param fileIndexId fileIndexId
     * @return String
     */
    @DeleteMapping("/remove")
    @Operation(summary = "删除文件")
    @Parameter(name = "fileIndexId", description = "文件索引的ID", required = true)
    ResultVO<String> remove(@RequestParam("fileIndexId") Long fileIndexId);

    /**
     * 有效期链接
     *
     * @param fileIndexId 文件索引的ID
     * @param expires 过期时间/s[默认是7天]
     * @return String
     */
    @GetMapping("/privatizedUrl")
    @Operation(summary = "有效期链接")
    @Parameter(name = "fileIndexId", description = "文件索引的ID", required = true)
    @Parameter(name = "expires", description = "过期时间/s[默认是7天]", required = false)
    ResultVO<String> privatizedUrl(@RequestParam("fileIndexId") Long fileIndexId
            , @RequestParam("expires") Integer expires);


    /**
     * 文件预览
     *
     * @param filePath 文件索引的path
     * @param storage  {@link StorageDict}
     * @param response HttpServletResponse
     */
    @GetMapping(value = OSSConstants.FTP_VIEWS_API_NAME + "/{storage}")
    @Operation(summary = "ftp文件预览")
    @Parameter(name = "filePath", description = "文件索引的path", required = true)
    @Parameter(name = "storage", description = "文件存储器类型字典", required = true)
    default void views(@PathVariable("storage") String storage, @RequestParam("filePath") String filePath, HttpServletResponse response) {
    }
}
